<script setup lang="ts">
import footerBtn from "@/components/footerBtn/index.vue";
import { useTheme } from "@/composables/useTheme";
import { onLoad, onReady, onShow } from "@dcloudio/uni-app";
import { hidePageNavInWechatBrowser } from "@/composables/useHiddenNavbar";
import { getPolicy, getPolicypcshow } from "@/api/pcApi";
import hideWechatMenu from "@/composables/hideWechatMenu";
onShow(() => {
  hideWechatMenu();
});
onReady(() => {
  hidePageNavInWechatBrowser();
});
onLoad(() => {
  getList();
});
let list = ref([]);
function getList() {
  getPolicy().then((res) => {
    let listData = res.data.gold_list;
    listData.forEach((item) => {
      item.list.forEach((_item) => {
        _item.is_pc_show = !!+_item.is_pc_show;
      });
    });
    list.value = listData;
    console.log(list.value);
  });
}
const { colorStyle } = useTheme();

async function switchChange(e, index, _index) {
  console.log(e, index, _index, "switchChange");
  let id = list.value[index].list[_index].id;
  const params = {
    id: id,
    is_pc_show: e ? 1 : 0,
  };

  await getPolicypcshow(params);
  getList()

}
function submitHandler() {
  list.value.forEach((item) => {
    item.is_pc_show = item.is_pc_show ? 0 : 1;
  });

  console.log("确认点击", list.value);
}
</script>
<template>
  <view class="wrap" :style="colorStyle">
    <view class="data_item" v-for="(item, index) in list" :key="index">
      <view class="title">{{ item.title }}</view>
      <view class="info">
        <view
          class="switch_item pad2030 flex_sb"
          v-for="(_item, _index) in item.list"
          :key="_index"
        >
          <text>{{ _item.code_name }}</text>
          <up-switch
            inactiveColor="rgb(178, 189, 194)"
            activeColor="var(--view-theme)"
            size="20"
            v-model="_item.is_pc_show"
            @change="(e) => switchChange(e, index, _index)"
          ></up-switch>
        </view>
      </view>
    </view>
    <view class="uni-p-b-98"></view>
    <!-- <footerBtn @submitHandler="submitHandler" title="保存设置" /> -->
  </view>
</template>

<style scoped lang="scss">
.wrap {
  .data_item {
    .title {
      color: rgba(125, 136, 140, 1);
      font-size: 28rpx;
      padding: 16rpx 30rpx;
    }
    .info {
      .switch_item {
        font-size: 30rpx;
        background: #fff;
        color: rgba(39, 38, 54, 1);
        &:not(:last-child) {
          margin-bottom: 2rpx;
        }
      }
    }
  }
}
</style>
